import { defineConfig, loadEnv } from 'vite'
const { resolve } = require('path')
import createVitePlugins from './vite/plugins'
// https://vitejs.dev/config/
export default ({ mode, command }) => {
  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_BASE, VITE_APP_ENV } = env
  const isProd = VITE_APP_ENV === 'production'
  return defineConfig({
    base: VITE_APP_BASE,
    build: {
      target: 'esnext',
      minify: true,
      terserOptions: {
        compress: {
          drop_console: true, // 删除 console
          drop_debugger: true, // 删除 debugger
        },
        format: {
          comments: false // 删除所有注释
        }
      },
      rollupOptions: {
        output: {
          manualChunks: {
            'element-plus': ['element-plus'],
            '@smallwei/avue': ['@smallwei/avue']
          },
        }
      }
    },
    optimizeDeps: {
      esbuildOptions: {
        target: 'esnext',
      },
    },
    resolve: {
      alias: {
        '~': resolve(__dirname, './'),
        "@": resolve(__dirname, "./src"),
        "components": resolve(__dirname, "./src/components"),
        "styles": resolve(__dirname, "./src/styles"),
        "utils": resolve(__dirname, "./src/utils"),
      }
    },
    server: {
      proxy: {
        // 匹配以 /api 开头的请求路径
        '/api': {
          // 代理目标地址
          // target: 'http://192.168.10.122:8080', //张
          target: 'http://192.168.10.243:8080', //刘
          // 允许跨域
          changeOrigin: true, 
          // 重写路径，将 /api 替换为空字符串
          rewrite: (path) => path.replace(/^\/api/, ''), 
        },
      },
    },
    plugins: createVitePlugins(env, command === 'build'),
  })
}
